const http = require("http");
const https = require("https");
const fs = require("fs");
const qs = require("querystring");

// 获取商品数据
// http://localhost:3000/api1?type=goods
// 注册接口
// http://localhost:3000/api1?type=register&username=用户名&password=密码

http.createServer((req,res)=>{
    if(req.url !== "/favicon.ico"){
        if(req.url.includes("/api1")){
            dataRequire(req, res)
        }else{
            fileRequire(req, res)
        }
    }
}).listen(3000);


function dataRequire(req, res){
    let str = "";
    req.on("data",(c)=>{
        str += c
    })
    req.on("end",()=>{
        let dataObj = null;
        if(str){
            dataObj = qs.parse(str);
        }else{
            dataObj = qs.parse(req.url.split("?")[1]);
        }
        // console.log(dataObj);
        switch(dataObj.type){
            case "goods":
                goods(req, res, dataObj);break;
            case "register":
                register(req,res,dataObj);break;
            case "login":
                login(req,res,dataObj);break;
        }
    })
}
// 模拟数据库存储用户信息
// const userMsg = [{username:"admin",password:1231},{},...];
const userMsg = [];

function register(req,res,dataObj){
    // console.log(userMsg);
    const flag = userMsg.some(val=>val.username === dataObj.username);
    const obj = {};
    if(flag){
        // 用户名重复，准备返回失败信息
        obj.code = 0;
        obj.title = "用户名重复";
        obj.data = "NOT DATA";
    }else{
        // 注册成功，添加用户信息
        userMsg.push({
            username:dataObj.username,
            password:dataObj.password
        })
        // 并准备返回成功信息
        obj.code = 1;
        obj.title = "注册成功";
        obj.data = "NOT DATA";
    }
    // 返回信息
    res.write(JSON.stringify(obj));
    res.end();
}

function login(req,res,dataObj){
    console.log(userMsg);
    // console.log("正在登录");
    let i=0;
    const flag = userMsg.some((val,idx)=>{  
        i=idx;
        return val.username === dataObj.username
    })
    
    const obj = {};
    if(flag){
        if(userMsg[i].password === dataObj.password){
            // 登录成功
            obj.code = 1;
            obj.title = "登录成功";
            obj.data = dataObj.username;
        }else{
            // 密码不对
            obj.code = 0;
            obj.title = "用户名密码不符";
            obj.data = "NOT DATA";
        }
    }else{
        // 账号不存在
        obj.code = 2;
        obj.title = "用户名不存在";
        obj.data = "NOT DATA";
    }
    res.write(JSON.stringify(obj));
    res.end();
}

function goods(req, res, dataObj){
    fs.readFile("./www/data/goods.json","utf-8",(err,data)=>{
        let msg;
        if(err){
            msg = {code:400,title:"请求失败",data:"NOT DATA"}
        }else{
            msg = {code:200,title:"请求成功",data:JSON.parse(data)}
        }
        res.write(JSON.stringify(msg));
        res.end();
    })
}

function fileRequire(req, res){
    const myURL = new URL("http://localhost:3000" + req.url);
    fs.readFile("./www" + myURL.pathname,(err, data)=>{
        if(err){
            res.write("404");
        }else{
            res.write(data);
        }
        res.end();
    })
}

